package com.ruoyi.system.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.ZtfMainTables;
import com.ruoyi.system.domain.ZtfVideoTable;
import com.ruoyi.system.domain.dto.ZtfMainTablesDTO;
import com.ruoyi.system.mapper.ZtfMainTablesMapper;
import com.ruoyi.system.mapper.ZtfVideoTableMapper;
import com.ruoyi.system.result.PageResult;
import com.ruoyi.system.service.IZtfMainTablesService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 智提分考试大师Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-30
 */
@Service
public class ZtfMainTablesServiceImpl implements IZtfMainTablesService
{
    @Autowired
    private ZtfMainTablesMapper ztfMainTablesMapper;

    @Autowired
    private ZtfVideoTableMapper ztfVideoTableMapper;

    /**
     * 查询智提分考试大师
     * 
     * @param ztfid 智提分考试大师主键
     * @return 智提分考试大师
     */
    @Override
    public ZtfMainTables selectZtfMainTablesByZtfid(Long ztfid)
    {
        return ztfMainTablesMapper.selectZtfMainTablesByZtfid(ztfid);
    }

    /**
     * 查询智提分考试大师列表
     * 
     * @param ztfMainTables 智提分考试大师
     * @return 智提分考试大师
     */
    @Override
    public List<ZtfMainTables> selectZtfMainTablesList(ZtfMainTables ztfMainTables)
    {

        List<ZtfMainTables> ztfMainTablesList= ztfMainTablesMapper.selectZtfMainTablesList(ztfMainTables);
//        for (ZtfMainTables ztfMainTables1 : ztfMainTablesList) {
//
//            //根据课程id查找视频
//             List<ZtfVideoTable> ztfVideoTableList =ztfVideoTableMapper.selectZtfVideoTableByztfId(ztfMainTables1.getZtfid());
//            List<ZtfVideoTable> ztfVideoTableList1=new ArrayList<>();
//            ztfVideoTableList.stream().forEach(ztfVideoTable1 -> {
//                if(ztfVideoTable1!=null) {
//                    //生成视频id
//                    ztfVideoTable1.setFileUrl(ztfVideoTable1.getVideoUrl());
//                    ztfVideoTable1.setVideoUrl(null);
//                    ztfVideoTable1.setVideoName(ztfVideoTable1.getTitle());
//                    ztfVideoTableList1.add(ztfVideoTable1);
//                }
//            });
//            ztfMainTables1.setVideoList(ztfVideoTableList1);
//        }
        return ztfMainTablesList;
    }

    /**
     * 新增智提分考试大师
     * 
     * @param ztfMainTables 智提分考试大师
     * @return 结果
     */
    @Override
    @Transactional
    public int insertZtfMainTables(ZtfMainTables ztfMainTables)
    {
       //获取相关视频数据
        List<ZtfVideoTable> videoTableList = ztfMainTables.getVideoList();

        //设置图片名称
        ztfMainTables.setZtfPicName(ztfMainTables.getZtfPicName());
        //用流处理视频数据
        videoTableList.stream().forEach(ztfVideoTable -> {
            //生成视频id
            String uuid = IdUtils.simpleUUID();
            ztfMainTables.setZtfVideoId(uuid);
            ZtfVideoTable ztfVideoTable1=new ZtfVideoTable();
            ztfVideoTable1.setVideoUrl(ztfVideoTable.getFileUrl());
            ztfVideoTable1.setUploadTime(new Date());
            ztfVideoTable1.setTitle(ztfVideoTable.getVideoName());
            ztfVideoTable1.setId(uuid);
            ztfVideoTable1.setZtfId(ztfMainTables.getZtfid());
            //课程分类id
            ztfVideoTable1.setZtfName(ztfMainTables.getZtfName());
            //新增视频相关数据
            ztfVideoTableMapper.insertZtfVideoTable(ztfVideoTable1);
        });
        //根据课程分类查询是否重复
       ZtfMainTables ztfMainTables1=ztfMainTablesMapper.selectZtfMainTablesByZtfName(ztfMainTables.getZtfName());
       if(ztfMainTables1!=null){
           if(ztfMainTables1.getZtfName().equals(ztfMainTables.getZtfName())){
               throw new ServiceException(ztfMainTables.getDicName()+"已存在");
           }
       }
        return ztfMainTablesMapper.insertZtfMainTables(ztfMainTables);
    }

    /**
     * 修改智提分考试大师
     * 
     * @param ztfMainTables 智提分考试大师
     * @return 结果
     */
    @Override
    public int updateZtfMainTables(ZtfMainTables ztfMainTables)
    {
        //从前端获取数据
        List<ZtfVideoTable> videoList = ztfMainTables.getVideoList();
        for (ZtfVideoTable ztfVideoTable : videoList) {
            ZtfVideoTable ztfVideoTable1=new ZtfVideoTable();
            if(ztfVideoTable.getId()==null){
                //生成视频id
                String uuid = IdUtils.simpleUUID();
                ztfVideoTable1.setVideoUrl(ztfVideoTable.getFileUrl());
                ztfVideoTable1.setUploadTime(new Date());
                ztfVideoTable1.setTitle(ztfVideoTable.getVideoName());
                ztfVideoTable1.setId(uuid);
                ztfVideoTable1.setZtfId(ztfMainTables.getZtfid());
                ztfVideoTable1.setZtfName(ztfMainTables.getZtfName());
                ztfVideoTableMapper.insertZtfVideoTable(ztfVideoTable1);
            }

        }
        return ztfMainTablesMapper.updateZtfMainTables(ztfMainTables);
    }

    /**
     * 批量删除智提分考试大师
     * 
     * @param ztfids 需要删除的智提分考试大师主键
     * @return 结果
     */
    @Override
    public int deleteZtfMainTablesByZtfids(String[] ztfids)
    {
        return ztfMainTablesMapper.deleteZtfMainTablesByZtfids(ztfids);
    }

    /**
     * 删除智提分考试大师信息
     * 
     * @param ztfid 智提分考试大师主键
     * @return 结果
     */
    @Override
    public int deleteZtfMainTablesByZtfid(Long ztfid)
    {
        return ztfMainTablesMapper.deleteZtfMainTablesByZtfid(ztfid);
    }

    /**
     * 课程列表分页（新）
     * @param ztfMainTablesDTO
     * @return
     */
    @Override
    public PageResult newSelectZtfMainTablesList(ZtfMainTablesDTO ztfMainTablesDTO) {
        PageHelper.startPage(ztfMainTablesDTO.getPageNum(), ztfMainTablesDTO.getPageSize());
        //该插件的规则 需要返回一个page对象
        Page<ZtfMainTables> page =  ztfMainTablesMapper.pageSelectZtfMainTables(ztfMainTablesDTO);

        //查询主表用于显示分页总数
//        Page<ZtfMainTables> mainTablesPage =  ztfAppMainTablesMapper.SelectByZtfMainTable();
        //调用PageHelper中的方法获取总条数和数据
        long total = page.getTotal();

        int pageNum=page.getPageNum();

        int pageSize=page.getPageSize();
        List<ZtfMainTables> ztfMainTablesList = page.getResult();

//        if(ztfMainTablesList.size()>0){
//            for (ZtfMainTables ztfMainTables : ztfMainTablesList) {
//                //根据分类id查询激活码状态
////                ZtfAppCodeNc ztfAppCodeNc=ztfAppCodeNcMapper.selectCodeByMianId(ztfAppMainTables.getZtfName(),userId);
////                if(ztfAppCodeNc!=null){
////                    ztfAppMainTables.setZtfCodeSort(ztfAppCodeNc.getStatus());
//                    List<ZtfVideoTable> videoList = ztfMainTables.getVideoList();
//                    //用stream流求和
//                    Integer totalOver=videoList.stream().mapToInt(ZtfVideoTable::getIsOver).sum();
//
//                    ztfMainTables.setZtfStudyClasshort(Long.valueOf(totalOver));
//
////                }
//                //处理激活码状态数据
//                long codeSort=ztfMainTables.getZtfCodeSort()==null? 0: ztfMainTables.getZtfCodeSort();
//                ztfMainTables.setZtfCodeSort(codeSort);
//            }
//        }

        return new PageResult(total,ztfMainTablesList,pageNum,pageSize);
    }

}

